package com.pan.listen;

import com.pan.base.ReflectUtil;
import com.pan.config.ApplicationConfig;
import com.pan.config.MappingConfig;
import com.pan.entity.ExecutiveInfo;
import com.pan.exe.Executive;
import com.pan.exe.driver.ExecutiveDriver;
import com.pan.exe.factory.ExecutiveFactory;

import java.util.HashMap;
import java.util.List;

public abstract class AbstractEventListener implements  EventListener{

    private ExecutiveDriver executiveDriver;
    private HashMap<String, Executive> executiveHashMap = new HashMap<>();

    protected ExecutiveDriver getExecutiveDriver() {
        return executiveDriver;
    }

    protected HashMap<String, Executive> getExecutiveHashMap() {
        return executiveHashMap;
    }

    protected void init(ExecutiveDriver executiveDriver, HashMap<String, Executive> executiveHashMap){
        this.executiveDriver = executiveDriver;
        this.executiveHashMap = executiveHashMap;
    }


    private ReflectUtil reflectUtil = ReflectUtil.INSTANCE;
    private ApplicationConfig applicationConfig = ApplicationConfig.INSTANCE;
    private MappingConfig mappingConfig = MappingConfig.INSTANCE;
    public AbstractEventListener(){
        String executiveDriverName = applicationConfig.getExecutiveDriverName();
        this.executiveDriver = reflectUtil.newInstance(executiveDriverName);
        String executiveFactoryName = applicationConfig.getExecutiveFactoryName();
        ExecutiveFactory executiveFactory = reflectUtil.newInstance(executiveFactoryName);
        List<ExecutiveInfo> executiveInfos = mappingConfig.getExecutiveInfos();
        for (ExecutiveInfo executiveInfo: executiveInfos){
            String key = executiveInfo.getKey();
            Executive executive = executiveFactory.create(executiveInfo);
            this.executiveHashMap.put(key,executive);
        }

    }

}
